package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpStatus;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.file.FileVO;
import com.minimalist.common.entity.lindi.po.LindiSubsidyApplicationPayRecord;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationpayrecord.LindiSubsidyApplicationPayRecordQueryVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationpayrecord.LindiSubsidyApplicationPayRecordVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiSubsidyApplicationPayRecordMapper;
import com.minimalist.lindi.service.LindiSubsidyApplicationPayRecordService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标交易平台_补贴申请支付记录表 服务层实现。
 *
 * @author 11987
 * @since 2025-06-09
 */
@Service
public class LindiSubsidyApplicationPayRecordServiceImpl extends ServiceImpl<
        LindiSubsidyApplicationPayRecordMapper, LindiSubsidyApplicationPayRecord>
        implements LindiSubsidyApplicationPayRecordService{

    private static final Logger logger = LoggerFactory.getLogger(LindiSubsidyApplicationPayRecordServiceImpl.class);

    @Autowired
    private LindiSubsidyApplicationPayRecordMapper mapper;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSubsidyApplicationPayRecord(
            LindiSubsidyApplicationPayRecordVO subsidyApplicationPayRecordVO){
        LindiSubsidyApplicationPayRecord record = BeanUtil.copyProperties(
                subsidyApplicationPayRecordVO, LindiSubsidyApplicationPayRecord.class);
        record.setSubsidyApplicationPayRecordId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(record, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                "添加补贴申请支付记录失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSubsidyApplicationPayRecordBySubsidyApplicationPayRecordId(
            Long subsidyApplicationPayRecordId){
        LindiSubsidyApplicationPayRecord record = mapper
                .selectSubsidyApplicationPayRecordBySubsidyApplicationPayRecordId(
                        subsidyApplicationPayRecordId);
        Assert.notNull(record, () -> new BusinessException("补贴申请支付记录不存在"));
        // 删除关联文件
        if (record.getFileId() != null){
            try{
                R<Void> result = fileFeignClient.deleteFile(record.getFileId());
                if (result.getCode() != HttpStatus.HTTP_OK){
                    logger.warn("删除关联文件失败，文件ID: {}, 错误信息: {}", record.getFileId(), result.getMessage());
                }
            } catch (Exception e){
                logger.warn("删除关联文件异常，文件ID: {}, 异常信息: {}", record.getFileId(), e.getMessage());
            }
        }
        int delete = mapper
                .deleteSubsidyApplicationPayRecordBySubsidyApplicationPayRecordId
                        (subsidyApplicationPayRecordId);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                "删除补贴申请支付记录失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSubsidyApplicationPayRecordBySubsidyApplicationPayRecordId(
            LindiSubsidyApplicationPayRecordVO subsidyApplicationPayRecordVO){
        LindiSubsidyApplicationPayRecord record = mapper
                .selectSubsidyApplicationPayRecordBySubsidyApplicationPayRecordId(
                        subsidyApplicationPayRecordVO.getSubsidyApplicationPayRecordId());
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请支付记录不存在"));
        BeanUtil.copyProperties(subsidyApplicationPayRecordVO, record
                , CopyOptions.create().ignoreNullValue());
        int update = mapper.update(record);
        Assert.isTrue(update == 1, () -> new BusinessException(
                "修改补贴申请支付记录失败"));
    }

    @Override
    public PageResp<LindiSubsidyApplicationPayRecordVO>
    getPageSubsidyApplicationPayRecordList(
            LindiSubsidyApplicationPayRecordQueryVO queryVO){
        // 分页查询
        Page<LindiSubsidyApplicationPayRecord> recordPage = mapper
                .selectPageSubsidyApplicationPayRecordList(queryVO);
        // 数据转换
        List<LindiSubsidyApplicationPayRecordVO> voList = BeanUtil.copyToList(
                recordPage.getRecords(), LindiSubsidyApplicationPayRecordVO.class);
        return new PageResp<>(voList, recordPage.getTotalRow());
    }

    @Override
    public LindiSubsidyApplicationPayRecordVO
    getSubsidyApplicationPayRecordBySubsidyApplicationPayRecordId(
            Long subsidyApplicationPayRecordId){
        LindiSubsidyApplicationPayRecord record = mapper
                .selectSubsidyApplicationPayRecordBySubsidyApplicationPayRecordId(
                        subsidyApplicationPayRecordId);
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请支付记录不存在"));
        LindiSubsidyApplicationPayRecordVO lindiSubsidyApplicationPayRecordVO =
                BeanUtil.copyProperties(record
                        , LindiSubsidyApplicationPayRecordVO.class);
        R<FileVO> result = fileFeignClient.getFileByFileId(record.getFileId());
        Assert.isTrue(result.getCode() == HttpStatus.HTTP_OK
                && result.getData() != null, () -> new BusinessException(
                "获取支付文件失败"));
        lindiSubsidyApplicationPayRecordVO.setFile(result.getData());
        return lindiSubsidyApplicationPayRecordVO;
    }

    @Override
    public PageResp<LindiSubsidyApplicationPayRecordVO> getSubsidyApplicationPayRecordListBySubsidyApplicationId(
            Long subsidyApplicationId){
        // 查询支付记录列表
        List<LindiSubsidyApplicationPayRecord> recordList = mapper
                .selectPayRecordListBySubsidyApplicationId(subsidyApplicationId);
        // 数据转换
        List<LindiSubsidyApplicationPayRecordVO> voList = BeanUtil.copyToList(
                recordList, LindiSubsidyApplicationPayRecordVO.class);
        return new PageResp<>(voList, voList.size());
    }
}